package com.woniuxy.controller;

import cn.hutool.core.util.StrUtil;
import com.nimbusds.jose.JOSEException;
import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.naming.ldap.Rdn;
import java.text.ParseException;

/**
 * @BelongsProject: pigTrotterStore
 * @BelongsPackage: com.woniuxy.controller
 * @Author: Quan
 * @Date: 2025/3/10 17:55
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {
	private UserService userService;

	@PostMapping("/login")
	public ResponseData<?> login(@RequestBody User user) {
		try {
			// 判断是否为空
			if (StrUtil.isBlank(user.getAccount()) || StrUtil.isBlank(user.getPassword())) {
				return ResponseData.fail(ResponseEnum.PARAM_ERROR);
			}
			return userService.login(user);
		} catch (JOSEException e) {
			throw new RuntimeException(e);
		}
	}

	@GetMapping("/addCart/{id}/{buyNum}")
	public ResponseData<?> addCart(@PathVariable("id") Integer id,
								   @PathVariable("buyNum") Integer buyNum) {
		try {
			return userService.addCart(id, buyNum);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	@GetMapping("/queryUserCartAndAddr")
	public ResponseData<?> queryUserCartAndAddr() {
		try {
			return userService.queryUserCartAndAddr();
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

}
